package ru.spb.osll.myfit.wikiverifier.verifier;

import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableCell;
import ru.spb.osll.myfit.wikiverifier.result.TableVerificationResult;
import ru.spb.osll.myfit.wikiverifier.result.VerificationResultType;

public class DoFixtureVerifier extends BasicFixtureVerifier {
	private Map<String, Class<?>> m_fixtures;

	public DoFixtureVerifier(Map<String, Class<?>> fixtures) {
		m_fixtures = fixtures;
	}

	@Override
	public TableVerificationResult verifyTable(Table table) {
		Iterator<Vector<TableCell>> tableIter = table.getData().iterator();

		if (!tableIter.hasNext()) {
			return new TableVerificationResult(VerificationResultType.FAILURE,
					table, "Test table is empty");
		}

		String className = getClassName(tableIter.next());
		if (m_fixtures.containsKey(className + "Fixture")) {
			className = className + "Fixture";
		}

		if (!m_fixtures.containsKey(className)) {
			return new TableVerificationResult(VerificationResultType.FAILURE,
					table, "Fixture class " + className + " not found");
		}

		Class<?> fixtureClass = m_fixtures.get(className);

		while (tableIter.hasNext()) {
			Vector<TableCell> line = tableIter.next();
			if (!verifyMethodLine(fixtureClass, line)) {
				return new TableVerificationResult(
						VerificationResultType.FAILURE, table, "Method "
								+ getMethodName(line) + " with "
								+ parameterCount(line)
								+ " parameters not found in class " + className);
			}
		}
		return new TableVerificationResult(VerificationResultType.OK);
	}
}